﻿using A.M.Storage.Infrastruecure;
using L.M.System.Domain.Entity;
using Newtonsoft.Json;
using O.M.Production.Domain.Entity;
using Public_Class_Library;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using W.M.Materiel.Domain.Entity;
using W.M.Materiel.Infrastruecure.DTO;
using W.M.Materiel.Infrastruecure.IRepository;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace W.M.Materiel.Infrastruecure.Repository
{
    public class Bom_management_tableRepository : BaseRepository<bom_management_table, string>, IBom_management_tableRepository
    {
        private readonly ISqlSugarClient _client;
        private readonly Cross_service_Helper cross_Service_Helper;

        public Bom_management_tableRepository(ISqlSugarClient client, Cross_service_Helper cross_Service_Helper = null) : base(client)
        {
            _client = client;
            this.cross_Service_Helper = cross_Service_Helper;
        }

        /// <summary>
        /// 显示BOM信息+查询+分页
        /// </summary>
        /// <param name="pageSize">页容量</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="name">查询条件:BOM编号</param>
        /// <returns>BOM信息</returns>
        public List<Product_BOMDTO> GetBOMrelation(int pageSize, int pageIndex, string? number, out int totalCount, out int pageCount)
        {
            //var obj = cross_Service_Helper.Get("http://10.31.59.5:8001/api/Table_of_unitsRead/GetTableUrl").Result;
            //var jsonObj = JsonConvert.DeserializeObject<List<table_of_units>>(obj);

            var list = (from a in _client.Queryable<bom_management_table>().ToList()
                            //join b in _client.Queryable<bom_item_relation_table>().ToList() on a.bom_number equals b.bom_number
                            //join c in _client.Queryable<product_management_table>().ToList() on b.product_material_numbers equals c.product_number
                            //join d in jsonObj
                            //on c.unit_id equals d.unit_number
                        where (string.IsNullOrEmpty(number) || a.bom_number.Contains(number))
                        select new Product_BOMDTO
                        {
                            bom_number = a.bom_number,
                            version_number = a.version_number,
                            finished_number = a.finished_number,
                            name_finished_product = a.name_finished_product,
                            //specification_model = c.specification_model,
                            type_finished_product = a.type_finished_product,
                            //unit_id = c.unit_id,
                            //unit_name = d.unit_name,
                            default_bom = a.default_bom,
                            daily_output = a.daily_output,

                        }).Distinct().ToList();
            totalCount = list.Count();
            pageCount = (int)Math.Ceiling(totalCount * 1.0 / pageSize);
            list = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).Distinct().ToList();
            return list;
        }

        /// <summary>
        /// 显示BOM的详情
        /// </summary>
        /// <param name="id">BOM编号</param>
        /// <returns></returns>
        public List<Details> GetProduct_BOMDTOs(string? id)
        {
            var list = (from a in _client.Queryable<bill_of_materials>().ToList()
                        join c in _client.Queryable<bom_item_relation_table>().ToList() on a.material_code equals c.product_material_numbers
                        join b in _client.Queryable<bom_management_table>().ToList() on c.bom_number equals b.bom_number
                        where (string.IsNullOrEmpty(id) || b.bom_number == id)
                        select new Details
                        {
                            bom_number = b.bom_number,
                            material_code = a.material_code,
                            alarm_days = a.alarm_days,
                            attachments = a.attachments,
                            attrition_rate = c.attrition_rate,
                            auxiliary_unit = a.auxiliary_unit,
                            bom_indicates_relationship_id = c.bom_indicates_relationship_id,
                            bom_topic = b.bom_topic,
                            brand = a.brand,
                            daily_output = b.daily_output,
                            default_bom = b.default_bom,
                            finished_number = b.finished_number,
                            material_attribute = a.material_attribute,
                            material_life = a.material_life,
                            material_name = a.material_name,
                            maximum_inventory = a.maximum_inventory,
                            minimum_inventory = a.minimum_inventory,
                            name_finished_product = b.name_finished_product,
                            number_parts = c.number_parts,
                            picture = a.picture,
                            product_material_numbers = c.product_material_numbers,
                            product_number = b.product_number,
                            purchase_price = a.purchase_price,
                            remark = a.remark,
                            serial_number = a.serial_number,
                            specification_model = a.specification_model,
                            status = a.status,
                            type_finished_product = b.type_finished_product,
                            unit_id = a.unit_id,
                            version_number = b.version_number
                        }).ToList();
            return list;
        }

        /// <summary>
        /// 工艺流程详细
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<CreatedInfoDTO> GetCreatedinfos(string? id)
        {
            var obj = cross_Service_Helper.Get("http://10.31.59.2:8088/api/Read_Process_Flow/process_flow").Result;
            var jsonObj = JsonConvert.DeserializeObject<List<process_flow>>(obj);

            var list = (from a in jsonObj
                        join b in _client.Queryable<bom_process_relationship_table>().ToList() on a.process_flow_id equals b.process_number
                        join c in _client.Queryable<bom_management_table>().ToList() on b.bom_number equals c.bom_number
                        where (string.IsNullOrEmpty(id) || c.bom_number == id)
                        select new CreatedInfoDTO
                        {
                            process_number = b.process_number,
                            bom_number = b.bom_number,
                            process_flow_id = a.process_flow_id,
                            attachment = a.attachment,
                            bom_process_id = b.bom_process_id,
                            bom_topic = c.bom_topic,
                            daily_output = c.daily_output,
                            default_bom = c.default_bom,
                            finished_number = c.finished_number,
                            name_finished_product = c.name_finished_product,
                            person_in_charge = a.person_in_charge,
                            process_flow_name = a.process_flow_name,
                            process_quantity = a.process_quantity,
                            product_number = c.product_number,
                            remark = c.remark,
                            remarks = a.remarks,
                            subordinate_department = a.subordinate_department,
                            type_finished_product = c.type_finished_product,
                            version_number = c.version_number,

                        }).ToList();
            return list;
        }

        /// <summary>
        /// 显示BOM信息
        /// </summary>
        /// <param name="pageSize">页容量</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="name">查询条件:BOM编号</param>
        /// <returns>BOM信息</returns>
        public List<Product_BOMDTO> GetBOMrelations()
        {
            var obj = cross_Service_Helper.Get("http://10.31.59.5:8001/api/Table_of_unitsRead/GetTableUrl").Result;
            var jsonObj = JsonConvert.DeserializeObject<List<table_of_units>>(obj);

            var list = (from a in _client.Queryable<bom_management_table>().ToList()
                        join b in _client.Queryable<bom_item_relation_table>().ToList() on a.bom_number equals b.bom_number
                        join c in _client.Queryable<product_management_table>().ToList() on b.product_material_numbers equals c.product_number
                        join d in jsonObj
                        on c.unit_id equals d.unit_number
                        select new Product_BOMDTO
                        {
                            bom_number = a.bom_number,
                            version_number = a.version_number,
                            finished_number = a.finished_number,
                            name_finished_product = a.name_finished_product,
                            specification_model = c.specification_model,
                            type_finished_product = a.type_finished_product,
                            unit_id = c.unit_id,
                            unit_name = d.unit_name,
                            default_bom = a.default_bom,
                            daily_output = a.daily_output,
                        }).ToList();
            return list;
        }
    }
}
